package app.game.common;

import ryulib.game.GameControl;
import ryulib.game.GamePlatformInfo;
import ryulib.game.HitArea;
import ryulib.graphic.AnimationCounter;
import ryulib.graphic.BitmapList;
import ryulib.graphic.Boundary;
import ryulib.graphic.Scroll;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import app.game.resource.ResourceAsteroid;
import app.game.resource.ResourceExplodingAsteroid;
import app.game.resource.ResourceSound;
import app.scene.SceneGame;
import app.scene.SceneManager;

public class Asteroid extends GameControl {
	
	private static final int _ANIMATIONINTERVAL = 100;
	private static final int _SPEED = 100;
//	private static final int _SPEED = 50;
	private static final int _HORIZONTAL_MARGINE = 5;
	private static final int _VERTICAL_MARGINE   = 5;

	public Asteroid(int x, int y) {
		super(null);
	
		_X = x;
		_Y = y;
		
		_HitArea.add(_HitBoundary);
	}

	private Canvas _Canvas = null;
	private Paint _Paint = null;
	
	private int _X = 0;
	private int _Y = 0;
	private Scroll _Scroll = new Scroll(_SPEED);
	
	private BitmapList _BitmapList = null;	
	private AnimationCounter _AnimationCounter = 
		new AnimationCounter(
				_ANIMATIONINTERVAL, 
				ResourceAsteroid.getInstance().getCount()
		);
	
	// �浹 �˻� �� �� �ڽ��� �����ϰ� �ִ� ������ �˷��ش�.  
	private HitArea _HitArea = new HitArea();

	// Boundary�� Rect�� ������ ����� �Ѵ�.  
	private Boundary _HitBoundary = new Boundary(0, 0, 0, 0);
	
	@Override
	protected HitArea getHitArea() {
		if (isExploding()) return null;
		
		_HitBoundary.setBoundary(
				_X + _HORIZONTAL_MARGINE, 
				_Y + _VERTICAL_MARGINE, 
				_X + ResourceAsteroid.SIZE - _HORIZONTAL_MARGINE*2, 
				_Y + ResourceAsteroid.SIZE - _VERTICAL_MARGINE*2
		);		
		
		return _HitArea;
	}
	
	public void explode() {
		Global.gamePlatform.getGamePlatformInfo().addMessage(this, Messages.ASTEROID_EXPLODE);
		
		// TODO : AsteroidAnimater�� ����
		_AnimationCounter.clear();
		_AnimationCounter.setSize(ResourceExplodingAsteroid.getInstance().getCount());
		_AnimationCounter.setAutoRewind(false);

		_BitmapList = ResourceExplodingAsteroid.getInstance();
		
		ResourceSound.getInstance().boom();
	}
	
	@Override
	protected void onStart(GamePlatformInfo platformInfo) {
		_Canvas = platformInfo.getCanvas();
		_Paint = platformInfo.getPaint();
		
		_BitmapList = ResourceAsteroid.getInstance();
	}
	
	@Override
	protected void onTick(GamePlatformInfo platformInfo) {
		long tick = platformInfo.getTick();
		
		_AnimationCounter.tick(tick);
		
		if (isExploding() == false) {
			// �ʴ� _Scroll.getSpeed() ��ŭ�� �ӵ��� ��� �̵� �Ѵ�.
			_X = _X - _Scroll.move(tick);

			// ȭ�鿡�� ������� ���� �Ѵ�.
			if (_X < (-ResourceAsteroid.SIZE)) this.delete(); 
		}
		
		if (SceneManager.getInstance().getScene() != SceneGame.getInstance()) {
			this.delete();
		}
	}
	
	@Override
	protected void onDraw(GamePlatformInfo platformInfo) {
		Bitmap bitmap = _BitmapList.getBitmap(_AnimationCounter.getIndex());
		
		if (bitmap != null) {
			_Canvas.drawBitmap(bitmap, _X, _Y, _Paint);
		} else {
			this.delete();
		}
	}
	
	public boolean isExploding() {
		return _BitmapList == ResourceExplodingAsteroid.getInstance();
	}
	
}
